package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Address> address = addressDao.selectByCondition(queryString);
        return new PageResult(address.getTotal(),address.getResult());
    }

    @Override
    public void add(Address address) {
        addressDao.add(address);
    }

    @Override
    public void delete(Integer id) {
        Long count = addressDao.findByAddressId(id);
        if(count > 0){
            throw  new RuntimeException("体检机构地址信息已被关联,无法删除");
        }
        addressDao.delete(id);
    }

    @Override
    public Address findById(Integer id) {
        Address address = addressDao.findById(id);
        return address;
    }
}
